{include file="public/header" /}
<div class="page">

    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>满即送</h3>
                <h5></h5>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>

<div class="dssc-form-default">
    <div class="alert alert-block">
        <h4>说明{$Think.lang.ds_colon}</h4>
        <ul>
            <li>{$Think.lang.mansong_add_explain1}</li>
<!--            <li>{$Think.lang.mansong_add_explain2}</li>-->
            <li>{$Think.lang.mansong_add_explain3}</li>
        </ul>
    </div>

<!--    <form method="get" name="formSearch" id="formSearch">-->
<!--        <div class="ds-search-form">-->
<!--            <dl style="width: 500px;">-->
<!--                <dt><i class="required">*</i>{$Think.lang.mansong_name}{$Think.lang.ds_colon}</dt>-->
<!--                <dd id="searchgc_td"></dd>-->
<!--                <input type="hidden" id="choose_gcid" name="choose_gcid" value="0"/>-->
<!--            </dl>-->
<!--            <div class="btn_group">-->
<!--                <a href="javascript:document.formSearch.submit();" class="btn " title="{$Think.lang.ds_query}">{$Think.lang.ds_query}</a>-->
<!--            </div>-->
<!--        </div>-->
<!--        <input type="hidden" value="" name="export">-->
<!--    </form>-->
    {empty name="mansong_info"}
    <form id="add_form" action="{:url('Promotionmansong/mansong_save')}" method="post">
        {else /}
        <form id="add_form" action="{:url('Promotionmansong/mansong_edit_save')}" method="post">
            <input type="hidden" name="mansong_id" value="{$mansong_info.mansong_id}">
            {/empty}
        <input id="level2_flag" type="hidden" value="false" />
        <input id="level3_flag" type="hidden" value="false" />
        <dl>
            <dt><i class="required">*</i>{$Think.lang.mansong_name}{$Think.lang.ds_colon}</dt>
            <dd>
                <input id="mansong_name" value="{present name='mansong_info'}{$mansong_info.mansong_name}{/present}" name="mansong_name" type="text" maxlength="25" class="w400 text"/>
                <span class="error-message"></span>
                <p class="hint">{$Think.lang.mansong_name_explain}</p>
            </dd>
        </dl>

        <dl>
            <dt><i class="required">*</i>{$Think.lang.start_time}{$Think.lang.ds_colon}</dt>
            <dd>
                <input id="start_time" value="{present name='mansong_info'}{$mansong_info.mansong_starttime|date="Y-m-d H:i:s",###}{/present}" name="start_time" type="text" class="text w130"/><em class="add-on"><i class="iconfont">&#xe8d6;</i></em>
                <span class="error-message"></span>
                <p class="hint">
                    {notpresent name="isPlatformStore"}{if $current_xianshi_quota.xianshiquota_starttime > 1}
                    {$Think.lang.xianshi_add_start_time_explain}{$current_xianshi_quota.xianshiquota_starttime|date="Y-m-d H:i:s",###}
                    {/if}{/notpresent}
                </p>
            </dd>
        </dl>
        <dl>
            <dt><i class="required">*</i>{$Think.lang.end_time}{$Think.lang.ds_colon}</dt>
            <dd>
                <input id="end_time" value="{present name='mansong_info'}{$mansong_info.mansong_endtime|date="Y-m-d H:i:s",###}{/present}" name="end_time" type="text" class="text w130"/><em class="add-on"><i class="iconfont">&#xe8d6;</i></em>
                <span class="error-message"></span>
                <p class="hint">
                    {notpresent name="isPlatformStore"}
                    {$Think.lang.xianshi_add_end_time_explain}{$current_xianshi_quota.xianshiquota_endtime|date="Y-m-d H:i:s",###}
                    {/notpresent}
                </p>
            </dd>
        </dl>

        {if empty($mansongrule_list)}
        <dl>
            <dt><i class="required">*</i>{$Think.lang.full_feed_rule}{$Think.lang.ds_colon}</dt>
            <dd>
                <input type="hidden" id="mansong_rule_count" name="rule_count">
                <ul id="mansong_rule_list" class="dssc-mansong-rule-list">
                </ul>
                <a href="javascript:void(0);" id="btn_add_rule" class="dssc-btn dssc-btn-acidblue"><i class="iconfont">&#xe6db;</i>{$Think.lang.add_rules}</a>
                <div id="div_add_rule" style="display:none;">
                    <div class="dssc-mansong-error"><span id="price_error" style="display:none;"><i class="iconfont">&#xe64c;</i>单笔订单满金额不能一致</span><span id="mansong_price_error" style="display:none;"><i class="iconfont">&#xe64c;</i>{$Think.lang.rule_amount_cannot_empty}</span><span id="mansong_discount_error" style="display:none;"><i class="iconfont">&#xe64c;</i>{$Think.lang.full_minus_mustless_rule}</span></div>
                    <div class="dssc-mansong-rule">
                        <span>{$Think.lang.level_price}&nbsp;<input id="mansong_price" name="mansong_price" type="text" class="text w50"><em class="add-on"><i class="iconfont">&#xe65c;</i></em>，</span>
                        <span>{$Think.lang.level_discount}&nbsp;<input id="mansong_discount" type="text" class="text w50"><em class="add-on"><i class="iconfont">&#xe65c;</i></em>，</span>
                        <span>{$Think.lang.gift_name}&nbsp;<a href="javascript:void(0);" id="btn_show_search_goods" class="dssc-btn"><i class="iconfont">&#xe753;</i>{$Think.lang.choose_gifts}</a></span> <div id="mansong_goods_item" class="gift"></div>

                        <div id="div_search_goods" class="div-goods-select mt10" style="display: none;">
                            <table class="search-form">
                                <tr>
                                    <th class="w150" style="width: 137px;">
                                        <strong>{$Think.lang.search_store_items}</strong>
                                    </th>
                                    <td class="w160">
                                        <input id="search_goods_name" type="text w150" class="text" name="goods_name" value=""/>
                                    </td>
                                    <td class="w70 tc">
                                        <a href="javascript:void(0);" id="btn_search_goods" class="dssc-btn"/><i class="iconfont">&#xe718;</i>{$Think.lang.ds_search}</a>
                                    </td>
                                    <td class="w10"></td>
                                    <td>
                                        <p class="hint">{$Think.lang.show_all_goods_sold}</p>
                                    </td>
                                </tr>
                            </table>
                            <a id="btn_hide_search_goods" class="close" href="javascript:void(0);">X</a>
                            <div id="div_goods_search_result" class="search-result" style="width:739px;"></div>
                        </div>
                    </div>
                    <div id="mansong_rule_error" style="display:none;">{$Think.lang.choose_least_one_promotion}</div>
                    <div class="mt10">
                        <a href="javascript:void(0);" id="btn_save_rule" class="dssc-btn dssc-btn-acidblue"><i class="iconfont">&#xe64d;</i>{$Think.lang.determine_rule_settings}</a>
                        <a href="javascript:void(0);" id="btn_cancel_add_rule" class="dssc-btn dssc-btn-orange"><i class="iconfont">&#xe754;</i>{$Think.lang.ds_cancel}</a></div>
                </div>
                <span class="error-message"></span>
<!--                <p class="hint">{$Think.lang.single_order_meets_amount}<br/>{$Think.lang.system_supports_rank_rules}</p>-->
            </dd>
        </dl>
            {else if/}
            <dl>
                <dt><i class="required">*</i>{$Think.lang.full_feed_rule}{$Think.lang.ds_colon}</dt>
                <dd>
                    <input type="hidden" id="mansong_rule_count" name="rule_count">
                    <ul id="mansong_rule_list" class="dssc-mansong-rule-list">
                        {foreach name="mansongrule_list" item="val"}
                        <!-- 判断是不是满送 -->
                        {if $val.goods_id > 0}
                            <li dstype="mansong_rule_item">
                                <span>单笔订单满<strong>{$val.mansongrule_price}</strong>元， </span>
                                <span>立减现金<strong>{$val.mansongrule_discount}</strong>元， </span>
                                <span>送价值<strong>{$val.new_price}</strong>元的商品({$val.mansong_goods_name})， </span>
                                <input type="hidden" id="mansongrule_id" name="mansongrule_id" value="{$val.mansongrule_id}">
                                <a href="javascript:dsLayerConfirm('{:url('Promotionmansong/del_rule',['rule_id'=>$val.mansongrule_id])}','{$Think.lang.ds_ensure_del}',{$val.mansong_id})" class="dsui-btn-del"><i class="iconfont"></i>{$Think.lang.ds_del}</a>
                            </li>
                        {else if/} 
                             <li dstype="mansong_rule_item">
                                <span>单笔订单满<strong>{$val.mansongrule_price}</strong>元， </span>
                                <span>立减现金<strong>{$val.mansongrule_discount}</strong>元， </span>
                                <input type="hidden" id="mansongrule_id" name="mansongrule_id" value="{$val.mansongrule_id}">
                                <a href="javascript:dsLayerConfirm('{:url('Promotionmansong/del_rule',['rule_id'=>$val.mansongrule_id])}','{$Think.lang.ds_ensure_del}',{$val.mansong_id})" class="dsui-btn-del"><i class="iconfont"></i>{$Think.lang.ds_del}</a>
                            </li>
                        {/if}   
                        {/foreach}
                    </ul>
                    {if $count != 3}
                    <a href="javascript:void(0);" id="btn_add_rule" class="dssc-btn dssc-btn-acidblue"><i class="iconfont">&#xe6db;</i>{$Think.lang.add_rules}</a>
                    {/if}
                    <div id="div_add_rule" style="display:none;">
                        <div class="dssc-mansong-error"><span id="price_error" style="display:none;"><i class="iconfont">&#xe64c;</i>单笔订单满金额不能一致</span><span id="mansong_price_error" style="display:none;"><i class="iconfont">&#xe64c;</i>{$Think.lang.rule_amount_cannot_empty}</span><span id="mansong_discount_error" style="display:none;"><i class="iconfont">&#xe64c;</i>{$Think.lang.full_minus_mustless_rule}</span></div>
                        <div class="dssc-mansong-rule">
                            <span>{$Think.lang.level_price}&nbsp;<input id="mansong_price" name="mansong_price" type="text" class="text w50"><em class="add-on"><i class="iconfont">&#xe65c;</i></em>，</span>
                            <span>{$Think.lang.level_discount}&nbsp;<input id="mansong_discount" type="text" class="text w50"><em class="add-on"><i class="iconfont">&#xe65c;</i></em>，</span>
                            <span>{$Think.lang.gift_name}&nbsp;<a href="javascript:void(0);" id="btn_show_search_goods" class="dssc-btn"><i class="iconfont">&#xe753;</i>{$Think.lang.choose_gifts}</a></span> <div id="mansong_goods_item" class="gift"></div>

                            <div id="div_search_goods" class="div-goods-select mt10" style="display: none;">
                                <table class="search-form">
                                    <tr>
                                        <th class="w150" style="width: 137px;">
                                            <strong>{$Think.lang.search_store_items}</strong>
                                        </th>
                                        <td class="w160">
                                            <input id="search_goods_name" type="text w150" class="text" name="goods_name" value=""/>
                                        </td>
                                        <td class="w70 tc">
                                            <a href="javascript:void(0);" id="btn_search_goods" class="dssc-btn"/><i class="iconfont">&#xe718;</i>{$Think.lang.ds_search}</a>
                                        </td>
                                        <td class="w10"></td>
                                        <td>
                                            <p class="hint">{$Think.lang.show_all_goods_sold}</p>
                                        </td>
                                    </tr>
                                </table>
                                <a id="btn_hide_search_goods" class="close" href="javascript:void(0);">X</a>
                                <div id="div_goods_search_result" class="search-result" style="width:739px;"></div>
                            </div>
                        </div>
                        <div id="mansong_rule_error" style="display:none;">{$Think.lang.choose_least_one_promotion}</div>
                        <div class="mt10">
                            <a href="javascript:void(0);" id="btn_save_rule" class="dssc-btn dssc-btn-acidblue"><i class="iconfont">&#xe64d;</i>{$Think.lang.determine_rule_settings}</a>
                            <a href="javascript:void(0);" id="btn_cancel_add_rule" class="dssc-btn dssc-btn-orange"><i class="iconfont">&#xe754;</i>{$Think.lang.ds_cancel}</a></div>
                    </div>
                    <span class="error-message"></span>
<!--                    <p class="hint">{$Think.lang.single_order_meets_amount}<br/>{$Think.lang.system_supports_rank_rules}</p>-->
                </dd>
            </dl>
            {/if}
            {if empty($mansong_info.gc_id)}
            <dl>
                <dt><i class="required">*</i>商品分类:</dt>
                <dd>
                    <span dstype="gc1">
                        <input type="hidden" id="choose_gcid" name="choose_gcid">
                {notempty name="gc_list"}
                <select dstype="gc"  data-param="{deep:1}" >
                    <option>{$Think.lang.ds_please_choose}</option>
                    {foreach name="gc_list" item="val"}
                    <option value="{$val.gc_id}">{$val.gc_name}</option>
                    {/foreach}
                </select>
                {/notempty}
            </span>
                    <span dstype="gc2" name="choose_gcid"></span>
                    <span dstype="gc3" name="choose_gcid"></span>
                </dd>
            </dl>
            {else if/}
            <dl>
                <dt>商品分类{$Think.lang.ds_colon}</dt>
                <dd id="show_name">
                        <span >
                        {$class_name}
                        <input type="button" value="编辑" id="gc_edit">
                        <input name="gc_ids" value="1"  type="hidden" id="gc_ids">
                    </span>
                </dd>

                <dd style="display: none" id="xian_name">
                    <span dstype="gc1">
                        <input type="hidden" id="choose_gcid" value="{$mansong_info.goods_class_id}" name="choose_gcid">
                        <input type="hidden" id="deep" value="" name="deep">
                {notempty name="gc_list"}
                <select dstype="gc"  data-param="{deep:1}" >
                    <option>{$Think.lang.ds_please_choose}</option>
                    {foreach name="gc_list" item="val"}
                    <option value="{$val.gc_id}">{$val.gc_name}</option>
                    {/foreach}
                </select>
                {/notempty}
            </span>
                    <span dstype="gc2" name="choose_gcid"></span>
                    <span dstype="gc3" name="choose_gcid"></span>
                </dd>
            </dl>
            {/if}
        <dl>
            <dt><i class="required">*</i>平台承担比例：</dt>
            <dd>
                <input id="platform_share" name="platform_share" type="text" maxlength="10" class="text w200"
                       value="{present name='mansong_info'}{$mansong_info.platform_share}{/present}"/>
                <span></span>
                <p style="margin-left: 397px;margin-top:-32px;font-size:14px;height:28px; ">%</p>
            </dd>
        </dl>
        <dl>
            <dt><i class="required">*</i>运营商承担比例：</dt>
            <dd>
                <input id="proportion_share" name="proportion_share" type="text" maxlength="10" class="text w200"
                       value="{present name='mansong_info'}{$mansong_info.proportion_share}{/present}"/>
                <span></span>
                <p style="margin-left: 397px;margin-top:-32px;font-size:14px;height:28px; ">%</p>
            </dd>
        </dl>
        <dl>
            <dt><i class="required">*</i>供应商承担比例：</dt>
            <dd>
                <input id="supplier_share" name="supplier_share" type="text" maxlength="10" class="text w200"
                       value="{present name='mansong_info'}{$mansong_info.supplier_share}{/present}"/>
                <span></span>
                <p style="margin-left: 397px;margin-top:-32px;font-size:14px;height:28px; ">%</p>
                <p class="hint">平台承担比例 + 运营商承担比例 + 供应商承担比例 = 100%。</p>
            </dd>
        </dl>
        <dl>
            <dt>{$Think.lang.text_remark}{$Think.lang.ds_colon}</dt>
            <dd>
                <textarea name="remark" rows="3" id="remark" maxlength="100" class="textarea w400">{present name='mansong_info'}{$mansong_info.mansong_remark}{/present}</textarea>
                <p class="hint">{$Think.lang.mansong_remark_explain}</p>
            </dd>
        </dl>
            <dl>
                <dt><i class="required">*</i>状态{$Think.lang.ds_colon}</dt>
                <dd class="onoff" style="margin-top: 5px;">
                    <label for="brand_recommend1" class="cb-enable {if $brand_array.mansong_state == '1'}selected{/if}" title="{$Think.lang.ds_yes}"><span>开启</span></label>
                    <label for="brand_recommend0" class="cb-disable {if $brand_array.mansong_state == '2'}selected{/if}" title="{$Think.lang.ds_no}"><span>关闭</span></label>
                    <input id="brand_recommend1" name="status" {if $brand_array.mansong_state == '1'}checked="checked"{/if} value="1" type="radio">
                    <input id="brand_recommend0" name="status" {if $brand_array.mansong_state == '2'}checked="checked"{/if} value="2" type="radio"></td>
                </dd>
            </dl>
        <div class="bottom">
            <input id="submit_button" type="submit" value="{$Think.lang.ds_submit}"  class="submit">
        </div>
    </form>
</div>
</div>

<script id="mansong_rule_template" type="text/html">
    <li dstype="mansong_rule_item">
        <span>{$Think.lang.level_price}<strong><%=price%></strong>元， </span>
        <span>{$Think.lang.level_discount}<strong><%=discount%></strong>元， </span>
        <%if(goods_id>0){%>
        <span>{$Think.lang.gift_name} <%==goods%></span>
        </dt><dd>平台设置价格：<input type="text" disabled  dstype="new_price" name="new_price" value="<%=new_price%>" style="width: 100px;"></dl>
        </dt><dd>数量：<input type="text" disabled  dstype="goods_num" name="goods_num" value="<%=goods_num%>" style="width: 100px;"></dl>
        <%}%>
        <input type="hidden" name="mansong_rule[]" value="<%=price%>,<%=discount%>,<%=goods_id%>,<%=new_price%>,<%=goods_num%>">
        <a dstype="btn_del_mansong_rule" href="javascript:void(0);" class="dssc-btn-mini dssc-btn-red"><i class="iconfont">&#xe725;</i>{$Think.lang.ds_del}</a>
    </li>
</script>


<script id="mansong_goods_template" type="text/html">
    <div dstype="mansong_goods" class="selected-mansong-goods">
        <img class="goods-thumb" src="<%=goods_image_url%>"/>
        <input dstype="mansong_goods_id" type="hidden" value="<%=goods_id%>">
        </dt><dd>礼品价格：<input type="text" id="new_price"  dstype="new_price" name="new_price" value="<%=new_price%>" style="width: 100px;"></dl>
        </dt><dd>数量：<input type="text" id="goods_num"  dstype="goods_num" name="goods_num" value="<%=goods_num%>" style="width: 100px;"></dl>
    </div>
    <a dstype="btn_del_mansong_goods" href="javascript:void(0);" class="dssc-btn-mini dssc-btn-red"><i class="iconfont">&#xe725;</i>{$Think.lang.delete_selected_gifts}</a>
</script>


<script src="{$Think.PLUGINS_SITE_ROOT}/template.min.js"></script>
<link rel="stylesheet" href="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.css">
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.js"></script>
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/i18n/jquery-ui-timepicker-zh-CN.js"></script>
<script type="text/javascript" src="{$Think.ADMIN_SITE_ROOT}/js/jquery.edit.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script type="text/javascript">
    $(function() {

        $('#gc_edit').click(function () {
            $('#show_name').remove();
            $('#xian_name').show();
        });

        // 查询下级分类，分类不存在显示当前分类绑定的规格
        $('select[dstype="gc"]').change(function() {
            $(this).parents('td:first').nextAll().html('');
            $('div[dstype="spec_ul"]').html('');
            $('div[dstype="spec_iframe"]').html('');
            getClassSpec($(this));
        });
    });

    // ajax选择商品分类
    function getClassSpec($this) {
        var id = parseInt($this.val());
        var data_str = '';
        eval('data_str =' + $this.attr('data-param'));
        var deep = data_str.deep;
        if (isNaN(id)) {
            // 清理分类
            clearClassHtml(parseInt(deep) + 1);
        }
        $.getJSON('ajax_class?id=' + id + '&deep=' + deep, function(data) {
            // $("#choose_gcid").val(data.gcid);
            $("#choose_gcid").val(id);
            $("#deep").val(deep);
            $('div[dstype="spec_iframe"]').empty();
            $('div[dstype="spec_ul"]').empty();
            // alert(data);
            if (data) {
                if (data.type == 'class') {
                    nextClass(data.data, data.deep);
                } else if (data.type == 'spec') {
                    specList(data.data, data.deep, data.gcid);
                }
            }
        });
    }

    // 下一级商品分类
    function nextClass(data, deep) {
        $('span[dstype="gc' + deep + '"]').html('').append('<select data-param="{deep:' + deep + '}"></select>')
            .find('select').change(function() {
            getClassSpec($(this));
        }).append('<option>{$Think.lang.ds_please_choose}</option>');
        $.each(data, function(i, n) {
            if (n != null) {
                $('span[dstype="gc' + deep + '"] > select').append('<option value="' + n.gc_id + '">' + n.gc_name + '</option>');
            }

        });


        // 清理分类
        clearClassHtml(parseInt(deep) + 1);
    }

    // 列出规格信息
    function specList(data, deep, gcid) {
        if (typeof(data) != 'undefined' && data != '') {
            var $_ul = $('<ul></ul>');
            $.each(data, function(i, n) {
                $_ul.append('<li><a href="javascript:void(0);" dstype="editSpec" data-param="{spid:' + n.sp_id + ',gcid:' + gcid + '}">{$Think.lang.ds_edit}' + n.sp_name + '{$Think.lang.specifications}</a></li>');
            });
            $_ul.find('a').click(function() {
                $_ul.find('li').removeClass('selected');
                $(this).parents('li:first').addClass('selected');
                editSpecvalue($(this));
            });
            $_ul.find('a:first').click();
            $('div[dstype="spec_ul"]').append($_ul);
        } else {
            $('div[dstype="spec_ul"]').append('<div class="warning-option"><i class="iconfont">&#xe64c;</i><span>{$Think.lang.category_cannot_add_specifications}</span></div>');
        }
        // 清理分类
        clearClassHtml(deep);
    }

    // 清理二级分类信息
    function clearClassHtml(deep) {
        switch (deep) {
            case 2:
                $('span[dstype="gc2"]').empty();
            case 3:
                $('span[dstype="gc3"]').empty();
                break;
        }
    }

    // ajax编辑规格值
    function editSpecvalue(o) {
        $('div[dstype="spec_iframe"]').html('');
        var data_str = '';
        eval('data_str =' + o.attr('data-param'));
        $_iframe = $('<iframe id="iframepage" name="iframepage" frameBorder=0 scrolling=no width="100%" height="630px" ' + 'src="{:url('Sellerspec/add_spec')}?spid=' + data_str.spid + '&gcid=' + data_str.gcid + '" ></iframe>'
    )
        ;
        $('div[dstype="spec_iframe"]').append($_iframe);
    }
    $(function(){
console.log({$gc_choose_json});
console.log({$gc_json});

        //商品分类
        init_gcselect({$gc_choose_json},{$gc_json});
        /* AJAX选择品牌 */
        $("#ajax_brand").brandinit();

        $('#dssubmit').click(function(){
            $('#formSearch').submit();
        });

        // 审核批量处理
        $('a[dstype="verify_batch"]').click(function(){
            ids_str = getItems();
            if (ids_str) {
                goods_verify(ids_str);
            }
        });
        // 违规下架批量处理
        $('a[dstype="lockup_batch"]').click(function(){
            ids_str = getItems();
            if (ids_str) {
                goods_lockup(ids_str);
            }
        });

        // ajax获取商品列表
        $('i[dstype="ajaxGoodsList"]').click(
            function(){

                var status = $(this).attr("status");
                if (status == 'open') {
                    $(this).attr('status', 'close');
                    $(this).removeClass('icon-jia').addClass('icon-jian');
                    var _parenttr = $(this).parents('tr');
                    var _commonid = $(this).attr('data-comminid');
                    var _div = _parenttr.next().find('.dssc-goods-sku');
                    if (_div.html() == '') {
                        $.getJSON('{:url("Goods/get_goods_list_ajax")}' , {commonid : _commonid}, function(date){
                            if (date != 'false') {
                                var _ul = $('<ul class="dssc-goods-sku-list"></ul>');
                                var res = eval('(' + date + ')');
                                for (var i = 0; i < res.length; i++)
                                {
                                    $('<li><div class="goods-thumb" title="{$Think.lang.goods_serial}：' + res[i].goods_serial + '"><a href="' + res[i].url + '" target="_blank"><image src="' + res[i].goods_image + '" ></a></div>' + res[i].goods_spec + '<div class="goods-price">{$Think.lang.goods_price}：<em title="￥' + res[i].goods_price + '">￥' + res[i].goods_price + '</em></div><div class="goods-storage">{$Think.lang.goods_storage}：<em title="' + res[i].goods_storage + '">' + res[i].goods_storage + '</em></div><a href="' + res[i].url + '" target="_blank" class="dssc-btn-mini">{$Think.lang.show_goods_detail}</a></li>').appendTo(_ul);
                                    _ul.appendTo(_div);
                                    _parenttr.next().show();
                                }

                                // 计算div的宽度
                                _div.css('width', document.body.clientWidth-54);

//                        _div.perfectScrollbar();
                            }
                        });
                    } else {
                        _parenttr.next().show()
                    }
                }else{
                    $(this).attr('status', 'open');
                    $(this).removeClass('icon-jian').addClass('icon-jia');
                    $(this).parents('tr').next().hide();
                }

            }
        );
    });
    $(document).ready(function(){
        $('#start_time').datetimepicker({dateFormat: 'yy-mm-dd'});
        $('#end_time').datetimepicker({dateFormat: 'yy-mm-dd'});

        jQuery.validator.methods.greaterThanDate = function(value, element, param) {
            var date1 = new Date(Date.parse(param.replace(/-/g, "/")));
            var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
            return date1 < date2;
        };
        jQuery.validator.methods.lessThanDate = function(value, element, param) {
            var date1 = new Date(Date.parse(param.replace(/-/g, "/")));
            var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
            return date1 > date2;
        };
        jQuery.validator.methods.greaterThanStartDate = function(value, element) {
            var start_date = $("#start_time").val();
            var date1 = new Date(Date.parse(start_date.replace(/-/g, "/")));
            var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
            return date1 < date2;
        };

        //页面输入内容验证
        $("#add_form").validate({
            errorPlacement: function(error, element){
                var error_td = element.parent('dd').children('span.error-message');
                error_td.append(error);
            },
            onfocusout: false,
            submitHandler:function(form){
                ds_ajaxpost('add_form', 'url', "{:url('Promotionmansong/index')}");
            },
            rules : {
                mansong_name : {
                    required : true
                },
                start_time : {
                    required : true,
                    // greaterThanDate : '{$start_time|date='Y-m-d H:i',###}'
                    greaterThanDate: "{:date('Y-m-d H:i',@$current_xianshi_quota['xianshiquota_starttime'])}"
                },
                end_time : {
                    required : true,
        {if !$isPlatformStore}

        lessThanDate : '{$end_time|date='Y-m-d H:i',###}',
            {/if}
        greaterThanStartDate : true
    },
        rule_count: {
            required: true,
                min: 1
        }
    },
        messages : {
            mansong_name : {
                required : '{$Think.lang.mansong_name_error}'
            },
            start_time : {
                required : '{:sprintf(lang("mansong_add_start_time_explain"),date("Y-m-d H:i",$start_time))}',
                    greaterThanDate : '{:sprintf(lang("mansong_add_start_time_explain"),date("Y-m-d H:i",$start_time))}'
            },
            end_time : {
                required : '{:sprintf(lang("mansong_add_end_time_explain"),date("Y-m-d H:i",$end_time))}',

                    {if !$isPlatformStore}
                lessThanDate : '{:sprintf(lang("mansong_add_end_time_explain"),date("Y-m-d H:i",$end_time))}',
                    {/if}
                greaterThanStartDate : '{$Think.lang.greater_than_start_time}'
            },
                rule_count: {
                    required: '{$Think.lang.add_rule_determine}',
                        min: '{$Think.lang.add_rule_determine}'
                }
            }
        });

        // 限时添加规则窗口
        $('#btn_add_rule').on('click', function() {
            $('#mansong_price').val('');
            $('#mansong_discount').val('');
            $('#mansong_goods_item').html('');
            $('#mansong_price_error').hide();
            $('#mansong_rule_error').hide();
            $('#div_add_rule').show();
            $('#btn_add_rule').hide();
        });
//通过节点获取name=name[] 的值
        function each_to_arr(obj){
            var parts_name_arr = [];
            obj.each(function(i){
                parts_name_arr.push(obj[i].value);
            })
            return parts_name_arr;
        }

        // 规则保存
        var arr = [];
        $('#btn_save_rule').on('click', function() {
            var mansong = {};
            mansong.price = Number($('#mansong_price').val());

            if(arr.indexOf($('#mansong_price').val()) >= 0){
                $('#price_error').show();
                return false;
            }
            $('#price_error').hide();
            arr.push($('#mansong_price').val());


            if(isNaN(mansong.price) || mansong.price <= 0) {
                $('#mansong_price_error').show();
                return false;
            } else {
                $('#mansong_price_error').hide();
            }
            mansong.discount = Number($('#mansong_discount').val());

            if(isNaN(mansong.discount) || mansong.discount < 0 || mansong.discount > mansong.price) {
                $('#mansong_discount_error').show();
                return false;
            } else {
                $('#mansong_discount_error').hide();
            }
            mansong.goods = $('#mansong_goods_item').find('[dstype="mansong_goods"]').html();

            mansong.goods_id = Number($('#mansong_goods_item').find('[dstype="mansong_goods_id"]').val());

            if(isNaN(mansong.goods_id)) {
                mansong.goods_id = 0;
            }
            if(mansong.discount == 0 && mansong.goods_id == 0) {
                $('#mansong_rule_error').show();
                return false;
            } else {
                $('#mansong_rule_error').hide();
            }

            mansong.new_price = Number($('#mansong_goods_item').find('[dstype="new_price"]').val());
            mansong.goods_num = Number($('#mansong_goods_item').find('[dstype="goods_num"]').val());

            var mansong_rule_item = template.render('mansong_rule_template', mansong);
          console.log(mansong_rule_item)
            $('#mansong_rule_list').append(mansong_rule_item);
            close_div_add_rule();
        });

        // 删除已添加的规则
        $('#mansong_rule_list').on('click', '[dstype="btn_del_mansong_rule"]', function() {
            $(this).parents('[dstype="mansong_rule_item"]').remove();
            close_div_add_rule();
        });
        // 删除已添加的规则
        $('#mansong_rule_list').on('click', '[dstype="btn_del_mansong_rule"]', function() {
            $(this).parents('[dstype="mansong_rule_item"]').remove();
            close_div_add_rule();
        });
        //彻底删除
        $('#mansong_rule_list').on('click', '[dstype="del_mansong_rule"]', function() {
           var id=  $('#mansongrule_id').val();
          $.post("{:url('Promotionmansong/del_rule')}",
                    // {goods_id: goods_id, xianshi_id: xianshi_id, xianshi_price: xianshi_price},
           {rule_id: id, },
                    function (data) {
                        if (data.result) {
                            layer.msg(data.message);
                            location.reload()
                        } else {
                            layer.msg(data.message);
                        }
                    },
                    'json');
        });

        // 取消添加规则
        $('#btn_cancel_add_rule').on('click', function() {
            close_div_add_rule();
        });

        // 关闭规则添加窗口
        function close_div_add_rule() {
            var rule_count = $('#mansong_rule_list').find('[dstype="mansong_rule_item"]').length;
            if( rule_count >= 3) {
                $('#btn_add_rule').hide();
            } else {
                $('#btn_add_rule').show();
            }
            $('#div_add_rule').hide();
            $('#mansong_rule_count').val(rule_count);
        }

        // 限时商品选择窗口
        $('#btn_show_search_goods').on('click', function() {
            $('#div_search_goods').show();
        });

        // 搜索商品
        $('#btn_search_goods').on('click', function() {
            var url = "{:url('Promotionmansong/search_goods')}";
            var datas = $.param({goods_name: $('#search_goods_name').val()});
            $('#div_goods_search_result').load(url,datas);
        });

        // 搜索商品翻页
        $('#div_goods_search_result').on('click', '.pagination li a', function() {
            $('#div_goods_search_result').load($(this).attr('href'));
            return false;
        });

        // 关闭商品选择窗口
        $('#btn_hide_search_goods').on('click', function() {
            $('#div_search_goods').hide();
        });

        // 选择商品
        $('#div_goods_search_result').on('click', '[dstype="btn_add_mansong_goods"]', function() {
            var goods = {};
            goods.goods_id = $(this).attr('data-goods-id');
            goods.goods_name = $(this).attr('data-goods-name');
            goods.goods_image_url = $(this).attr('data-goods-image-url');
            goods.goods_url = $(this).attr('data-goods-url');
            goods.new_price = $(this).attr('data-new_price');
            goods.goods_num = $(this).attr('data-goods_num');
            var mansong_goods_item = template.render('mansong_goods_template', goods);
            $('#mansong_goods_item').html(mansong_goods_item);
            $('#div_search_goods').hide();
        });

        // 删除以选的商品
        $('#mansong_goods_item').on('click', '[dstype="btn_del_mansong_goods"]', function() {
            $('#mansong_goods_item').html('');
        });

    });

</script>
